package md.mru.frontend.beans;

import md.mru.backend.core.dtos.*;
import md.mru.backend.core.service.EmploymentDataService;
import org.primefaces.context.RequestContext;
import org.primefaces.event.RowEditEvent;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sergiu.marin
 * @since 29.03.2014
 */
@ManagedBean(name = "employmentDataBean")
@SessionScoped
public class EmploymentDataBean extends BackingBean {

    private static final long serialVersionUID = 3674781217281272940L;

    @ManagedProperty(value = "#{employmentDataService}")
    private EmploymentDataService dataService;

    private List<ContractDto> contracts = new ArrayList<>();
    private List<ContractAgreementDto> contractAgreements = new ArrayList<>();
    private List<WorkExperienceDto> workExperiences = new ArrayList<>();
    private List<WorkCategoryDto> workCategories = new ArrayList<>();
    private List<SalaryDto> salaries = new ArrayList<>();
    private List<SalaryLevelDto> salaryLevels = new ArrayList<>();
    private List<SalaryStepDto> salarySteps = new ArrayList<>();

    public void addNewContract(String editRowId) {
        ContractDto c = new ContractDto();
        contracts.add(c);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("ctc-formId", "ctcData", editRowId));
    }

    public void addNewContractAgreement(String editRowId) {
        ContractAgreementDto c = new ContractAgreementDto();
        contractAgreements.add(c);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("ca-formId", "caData", editRowId));
    }

    public void addNewWorkExperience(String editRowId) {
        WorkExperienceDto w = new WorkExperienceDto();
        workExperiences.add(w);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("we-formId", "weData", editRowId));
    }

    public void addNewSalary(String editRowId) {
        SalaryDto sr = new SalaryDto();
        salaries.add(sr);
        RequestContext.getCurrentInstance().execute(this.constructOnCompleteScript("sr-formId", "srData", editRowId));
    }

    public void onContractDataEdit(RowEditEvent event) {
        ContractDto contract = ((ContractDto) event.getObject());
        contract.setId(dataService.saveContract(contract));
    }

    public void onContractAgreementDataEdit(RowEditEvent event) {
        ContractAgreementDto contractAgreement = ((ContractAgreementDto) event.getObject());
        contractAgreement.setId(dataService.saveContractAgreement(contractAgreement));
    }

    public void onWorkExperienceDataEdit(RowEditEvent event) {
        WorkExperienceDto workExperience = ((WorkExperienceDto) event.getObject());
        workExperience.setId(dataService.saveWorkExperience(workExperience));
    }

    public void onSalaryDataEdit(RowEditEvent event) {
        SalaryDto salary = ((SalaryDto) event.getObject());
        salary.setId(dataService.saveSalary(salary));
    }

    public void deleteContract(ContractDto c) {
        dataService.deleteContract(c);
        this.contracts.remove(c);
    }

    public void deleteContractAgreement(ContractAgreementDto c) {
        dataService.deleteContractAgreement(c);
        this.contractAgreements.remove(c);
    }

    public void deleteWorkExperience(WorkExperienceDto w) {
        dataService.deleteWorkExperience(w);
        this.workExperiences.remove(w);
    }

    public void deleteSalary(SalaryDto sr) {
        dataService.deleteSalary(sr);
        this.salaries.remove(sr);
    }


    public List<ContractDto> getContracts() {
        return contracts;
    }

    public void setContracts(List<ContractDto> contracts) {
        this.contracts = contracts;
    }

    public List<ContractAgreementDto> getContractAgreements() {
        return contractAgreements;
    }

    public void setContractAgreements(List<ContractAgreementDto> contractAgreements) {
        this.contractAgreements = contractAgreements;
    }

    public List<WorkExperienceDto> getWorkExperiences() {
        return workExperiences;
    }

    public void setWorkExperiences(List<WorkExperienceDto> workExperiences) {
        this.workExperiences = workExperiences;
    }

    public List<WorkCategoryDto> getWorkCategories() {
        if (workCategories.isEmpty()) {
            workCategories = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                WorkCategoryDto wc = new WorkCategoryDto();
                wc.setId(i);
                wc.setName("Categorie_" + 1);
                workCategories.add(wc);
            }
            getObjectMappingConverter().mapObjectList(workCategories);
        }
        return workCategories;
    }

    public void setWorkCategories(List<WorkCategoryDto> workCategories) {
        this.workCategories = workCategories;
    }

    public List<SalaryDto> getSalaries() {
        return salaries;
    }

    public void setSalaries(List<SalaryDto> salaries) {
        this.salaries = salaries;
    }

    public List<SalaryLevelDto> getSalaryLevels() {
        if (salaryLevels.isEmpty()) {
            salaryLevels = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                SalaryLevelDto sl = new SalaryLevelDto();
                sl.setId(i);
                sl.setName("Salary_level_" + 1);
                salaryLevels.add(sl);
            }
            getObjectMappingConverter().mapObjectList(salaryLevels);
        }
        return salaryLevels;
    }

    public void setSalaryLevels(List<SalaryLevelDto> salaryLevels) {
        this.salaryLevels = salaryLevels;
    }

    public List<SalaryStepDto> getSalarySteps() {
        if (salarySteps.isEmpty()) {
            salarySteps = new ArrayList<>(10);
            for (int i = 1; i <= 10; i++) {
                SalaryStepDto ss = new SalaryStepDto();
                ss.setId(i);
                ss.setName("Salary_step_" + 1);
                salarySteps.add(ss);
            }
            getObjectMappingConverter().mapObjectList(salarySteps);
        }
        return salarySteps;
    }

    public void setSalarySteps(List<SalaryStepDto> salarySteps) {
        this.salarySteps = salarySteps;
    }

    public void setDataService(EmploymentDataService dataService) {
        this.dataService = dataService;
    }
}
